/**
 * Cube.util.Observable is an interface to manage events etc. on objects.
 * 
 * @class Cube.util.Observable 
 */
Cube.define('Cube.util.Observable', {
	
	events: {},
	
	/**
	 * Fires the event
	 * 
	 * @param {String} eventName 
	 */
	fireEvent: function(eventName) {
		var args = Cube.Array.slice(arguments, 1);
		
		if (typeof this.events[eventName] !== "undefined")
		{
			Cube.Array.each(this.events[eventName].listeners, function(fn, index) {
				this.events[eventName].listeners.splice(index, 1);
				fn.apply(this, args);
			}, this);
		}
	},
	
	/**
	 * Add event listener
	 *  
 	 * @param {String} eventName
 	 * @param {Function} fn
     * @param {Object} scope
	 */
	addListener: function(eventName, fn, scope) {
		this.events[eventName] = this.events[eventName] || { listeners: [] };
		this.events[eventName].listeners.push(fn);
	},
	
	/**
	 * Add multiple event listeners. This is a shorthand for {@link Cube.util.Observable#addListener}.
	 *  
 	 * @param {Object} object
 	 * @param {Object} scope
	 */
	on: function(object, scope)
	{
		for (i in object)
		{
			this.addListener(i, object[i], scope);
		}
	}
});
